<?php
class FileController extends Controller
{
	
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('baiduupload',),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	public function actionBaiDuUpload()
	{
		UtilTools::writeToFile($_FILES);
		
		
		
	    //上传配置
	    $config = array(
	        "uploadPath"=>'.'.Yii::app()->params['uploadEditorPath'], //"public/uploadfiles/",                          //保存路径
	        "fileType"=>array(".gif",".png",".jpg",".jpeg",".bmp"),   //文件允许格式
	        "fileSize"=>1000                                          //文件大小限制，单位KB
	    );
	    
	    //文件上传状态,当成功时返回SUCCESS，其余值将直接返回对应字符窜并显示在图片预览框，同时可以在前端页面通过回调函数获取对应字符窜
	    $state = "SUCCESS";
	
	    $title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
	    $path  = $config['uploadPath'];
	    if(!is_dir($path)){       
	        UtilTools::createFolder($path);
	    }
	    
	    UtilTools::writeToFile($config);
	    
	    //格式验证
	    $current_type = strtolower(strrchr($_FILES["picdata"]["name"], '.'));
	    if(!in_array($current_type, $config['fileType'])){
	        $state = "不支持的图片类型！";
	    }
	    //大小验证
	    $file_size = 1024 * $config['fileSize'];
	    if( $_FILES["picdata"]["size"] > $file_size ){
	        $state = "图片大小超出限制！";
	    }    
	  
	    //保存图片
	    if($state == "SUCCESS"){
	        $tmp_file=$_FILES["picdata"]["name"];
	        $file = $path.rand(1,10000).time().strrchr($tmp_file,'.');
	        
	        $info = pathinfo($_FILES['picdata']['name']);
	        
	        UtilTools::writeToFile($info);
	          
	        
	        $model = new File();
//			$model->pid = Lookup::model()->getUserAdvertisementAlbum(Yii::app()->user->id)->id;
			$model->file_size = $_FILES['picdata']['size'];
			$model->file_ext = strtolower($info['extension']);
			$model->file_name = $info['filename'];
			$model->file_created = time();
//			$model->mime = File::model()->getMimeType($model->ext, $_FILES['picdata']['type']);
			
			UtilTools::writeToFile($model->attributes);
			
//			UtilHelper::writeToFile($model->attributes);
						
//			$file = File::model()->generateFileName($model, 'advertisement', true);
	        
//	        UtilHelper::writeToFile($file, './public/testd.txt','a+');
	        
			UtilTools::writeToFile($file);
//	        if($model->save())
//	        {
	        	$result = move_uploaded_file($_FILES["picdata"]["tmp_name"],$file);
	        	if(!$result){
		            $state = "图片保存失败！";
		        }
//	        }
//	        else 
//	        {
//	        	$state = "数据保存失败！";
//	        }
	        
	        
	        
	        

	    }
	    //向浏览器返回数据json数据
	    $file= str_replace(array('../','./'),'',$file);  //为方便理解，替换掉所有类似../和./等相对路径标识
	    
	    $str = "{'url':'".$file."','title':'".$title."','state':'".$state."'}";
   
	    echo $str;		
	}
}
?>